package com.quantstack.llcommonlibrary.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.quantstack.llcommonlibrary.R;

/**
 * @author wu_ming_zhi_bei
 * @date 2021/2/26 13:31
 * @Notes 菜单栏
 */
public class MenuItem extends LinearLayout {
    private Context context;
    private TextView tvTitle;
    private TextView tvState;
    private ImageView ivIcon;
    public MenuItem(Context context) {
        this(context,null);
    }

    public MenuItem(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MenuItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        //加载布局
        LayoutInflater.from(context).inflate(R.layout.layout_manu_item,this);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvState = (TextView) findViewById(R.id.tv_state);
        ivIcon = (ImageView) findViewById(R.id.iv_icon);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MenuItem);
        int titleSize = a.getDimensionPixelSize(R.styleable.MenuItem_titleSize,16);
        int titleColor = a.getColor(R.styleable.MenuItem_titleColor,getResources().getColor(R.color.c444));
        String title = a.getString(R.styleable.MenuItem_title);
        tvTitle.setText(title);
        tvTitle.setTextSize(titleSize);
        tvTitle.setTextColor(titleColor);

        int stateSize = a.getDimensionPixelSize(R.styleable.MenuItem_stateSize,14);
        int stateColor = a.getColor(R.styleable.MenuItem_stateColor,getResources().getColor(R.color.c999));
        String state = a.getString(R.styleable.MenuItem_state);
        tvState.setText(state);
        tvState.setTextSize(stateSize);
        tvState.setTextColor(stateColor);

        //图标
        Drawable icon = a.getDrawable(R.styleable.MenuItem_icon);
        if(icon!=null){
            ivIcon.setImageDrawable(icon);
        }else{
            ivIcon.setImageResource(R.mipmap.checkbox_normal);
        }
        a.recycle();
    }

    //设置图标
    public void setIocn(Drawable drawable){
        ivIcon.setImageDrawable(drawable);
    }

    /**
     * 设置状态
     */
    public void setState(boolean state,String msg){
        tvState.setText(msg);
        if(state){
            ivIcon.setImageResource(R.mipmap.checkbox_check);
        }else{
            ivIcon.setImageResource(R.mipmap.checkbox_normal);
        }
    }

}
